package com.cn.hu.entity;

import java.sql.Timestamp;
import java.util.List;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import static org.hibernate.criterion.Example.create;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;

/**
 * A data access object (DAO) providing persistence and search support for Plan
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.cn.hu.entity.Plan
 * @author MyEclipse Persistence Tools
 */
@Transactional
public class PlanDAO {
	private static final Logger log = LoggerFactory.getLogger(PlanDAO.class);
	// property constants
	public static final String CUSTOMER_ID = "customerId";
	public static final String DEP_ID = "depId";
	public static final String CONTACTS_ID = "contactsId";
	public static final String DEPARMENT_NAME = "deparmentName";
	public static final String CONTACTS_NAME = "contactsName";
	public static final String EMP_ID = "empId";
	public static final String EMPLOYEE_NAME = "employeeName";
	public static final String PLAN_UNIT = "planUnit";
	public static final String SUPPLIER_ID = "supplierId";
	public static final String MANUFACTOR = "manufactor";
	public static final String TEL = "tel";
	public static final String GOODS_NAME = "goodsName";
	public static final String GOODS_TYPE = "goodsType";
	public static final String GOODS_UNIT = "goodsUnit";
	public static final String GOODS_NUM = "goodsNum";
	public static final String GOODS_PRICE = "goodsPrice";
	public static final String GOODS_TOTAL = "goodsTotal";
	public static final String GOODS_ORIGINAL = "goodsOriginal";
	public static final String PLAN_STATUS = "planStatus";
	public static final String PAPER_PRICE = "paperPrice";
	public static final String PAPER_TOTAL_PRICE = "paperTotalPrice";
	public static final String STATUS = "status";
	public static final String BAK = "bak";

	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	protected void initDao() {
		// do nothing
	}

	public void save(Plan transientInstance) {
		log.debug("saving Plan instance");
		try {
			getCurrentSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Plan persistentInstance) {
		log.debug("deleting Plan instance");
		try {
			getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Plan findById(java.lang.Integer id) {
		log.debug("getting Plan instance with id: " + id);
		try {
			Plan instance = (Plan) getCurrentSession().get(
					"com.cn.hu.entity.Plan", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Plan> findByExample(Plan instance) {
		log.debug("finding Plan instance by example");
		try {
			List<Plan> results = (List<Plan>) getCurrentSession()
					.createCriteria("com.cn.hu.entity.Plan")
					.add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Plan instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Plan as model where model."
					+ propertyName + "= ?";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<Plan> findByCustomerId(Object customerId) {
		return findByProperty(CUSTOMER_ID, customerId);
	}

	public List<Plan> findByDepId(Object depId) {
		return findByProperty(DEP_ID, depId);
	}

	public List<Plan> findByContactsId(Object contactsId) {
		return findByProperty(CONTACTS_ID, contactsId);
	}

	public List<Plan> findByDeparmentName(Object deparmentName) {
		return findByProperty(DEPARMENT_NAME, deparmentName);
	}

	public List<Plan> findByContactsName(Object contactsName) {
		return findByProperty(CONTACTS_NAME, contactsName);
	}

	public List<Plan> findByEmpId(Object empId) {
		return findByProperty(EMP_ID, empId);
	}

	public List<Plan> findByEmployeeName(Object employeeName) {
		return findByProperty(EMPLOYEE_NAME, employeeName);
	}

	public List<Plan> findByPlanUnit(Object planUnit) {
		return findByProperty(PLAN_UNIT, planUnit);
	}

	public List<Plan> findBySupplierId(Object supplierId) {
		return findByProperty(SUPPLIER_ID, supplierId);
	}

	public List<Plan> findByManufactor(Object manufactor) {
		return findByProperty(MANUFACTOR, manufactor);
	}

	public List<Plan> findByTel(Object tel) {
		return findByProperty(TEL, tel);
	}

	public List<Plan> findByGoodsName(Object goodsName) {
		return findByProperty(GOODS_NAME, goodsName);
	}

	public List<Plan> findByGoodsType(Object goodsType) {
		return findByProperty(GOODS_TYPE, goodsType);
	}

	public List<Plan> findByGoodsUnit(Object goodsUnit) {
		return findByProperty(GOODS_UNIT, goodsUnit);
	}

	public List<Plan> findByGoodsNum(Object goodsNum) {
		return findByProperty(GOODS_NUM, goodsNum);
	}

	public List<Plan> findByGoodsPrice(Object goodsPrice) {
		return findByProperty(GOODS_PRICE, goodsPrice);
	}

	public List<Plan> findByGoodsTotal(Object goodsTotal) {
		return findByProperty(GOODS_TOTAL, goodsTotal);
	}

	public List<Plan> findByGoodsOriginal(Object goodsOriginal) {
		return findByProperty(GOODS_ORIGINAL, goodsOriginal);
	}

	public List<Plan> findByPlanStatus(Object planStatus) {
		return findByProperty(PLAN_STATUS, planStatus);
	}

	public List<Plan> findByPaperPrice(Object paperPrice) {
		return findByProperty(PAPER_PRICE, paperPrice);
	}

	public List<Plan> findByPaperTotalPrice(Object paperTotalPrice) {
		return findByProperty(PAPER_TOTAL_PRICE, paperTotalPrice);
	}

	public List<Plan> findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	public List<Plan> findByBak(Object bak) {
		return findByProperty(BAK, bak);
	}

	public List findAll() {
		log.debug("finding all Plan instances");
		try {
			String queryString = "from Plan";
			Query queryObject = getCurrentSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Plan merge(Plan detachedInstance) {
		log.debug("merging Plan instance");
		try {
			Plan result = (Plan) getCurrentSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Plan instance) {
		log.debug("attaching dirty Plan instance");
		try {
			getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Plan instance) {
		log.debug("attaching clean Plan instance");
		try {
			getCurrentSession().buildLockRequest(LockOptions.NONE).lock(
					instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static PlanDAO getFromApplicationContext(ApplicationContext ctx) {
		return (PlanDAO) ctx.getBean("PlanDAO");
	}
}